<?php

class SpecialTempController extends Controller
{
	public function actionIndex()
	{
		//TODO: always show list 10, 2013.02.11.Bocheng
		$sql_template = 'SELECT t.id, t.id_merchant, t.url_picture, t.content, t.price, t.value, t.discount
			FROM  tbl_merchant_special_template t
			WHERE t.id_merchant=:id_merchant 
			ORDER BY t.access_time DESC
			LIMIT 10 ';
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql_template);
		$rows = $command->queryAll(true, array(
			':id_merchant' => Yii::app()->user->id,
		));
		$connection->active = false;
		
		$this->render('index', array('rows' => $rows));
	}
	
	/**
	 * 
	 * @param int $id	Special Template Id
	 */
	public function actionForm($id = null)
	{
		$model = empty($id) ? new SpecialTemplateForm() : $this->_loadModel((int)$id);
		// if it is ajax validation request
		if (isset($_POST['ajax']) && $_POST['ajax'] === 'template-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		
		// collect user input data
		if (isset($_POST['SpecialTemplateForm'])) {
			$model->id_merchant = Yii::app()->user->id;
			$model->content = strip_tags($_POST['SpecialTemplateForm']['content']);
			//set the url_picture
			$model->url_picture = $_POST['SpecialTemplateForm']['url_picture'];
			
			$model->price = $_POST['SpecialTemplateForm']['price'];
			$model->value = $_POST['SpecialTemplateForm']['value'];
			$model->discount = 1 - $model->price/$model->value;
			$model->access_time = new CDbExpression('NOW()');
			
			if ($model->save()) {
				$url = $this->createUrl('index', array('id'=>$model->id));
				Yii::app()->request->redirect($url);
			} else {
				Yii::app()->user->setFlash('saved', 'System error was happen when you saved!');
				$this->refresh();
			}
		} else {
			$this->render('form', array('model' => $model));
		}
	}

	public function actionView()
	{
		$this->render('view');
	}
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	protected function _loadModel($id) {
		$model = SpecialTemplateForm::model()->findByPk(
			$id,
			'id_merchant=:id_merchant',
			array('id_merchant'=>Yii::app()->user->id)
		);
		if ($model===null)
			throw new CHttpException(404, 'The requested page does not exist.');
		return $model;
	}
	
	/**
	 * @return array action filters
	 */
	public function filters() {
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}
	
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules() {
		return array(
			array('allow', // allow authenticated user to perform below actions
				'actions'=>array('index', 'form', 'view'),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'actions'=>array('index', 'form', 'view'),
				'users'=>array('*'),
			),
		);
	}
}